/*
 * ---------------------------------------------------------------------
 * Copyright (c) 2014, 2015 All Right Reserved
 * This software is developed for study and improve coding skill ...
 *
 * Project:  Excellent Coding< EC >
 *
 * ecMutex.h
 * This file for EC ecMutex interface implementation.
 *
 * Eamil:   epengao@126.com
 * Author:  Peter Gao
 * Version: Intial first version.
 * --------------------------------------------------------------------
 */

#include "ecMutex.h"

namespace ec {

ecMutex::ecMutex()
{
#ifdef _WIN32
    m_hMutex = CreateMutex(0, false, NULL); 
#else
    pthread_mutex_init(&m_hMutex, EC_NULL);  
#endif
}

ecMutex::~ecMutex()
{
#ifdef _WIN32
    CloseHandle(m_hMutex);
#else
    pthread_mutex_destroy(&m_hMutex);
#endif
}

void ecMutex::Lock()
{
#ifdef _WIN32
    WaitForSingleObject(m_hMutex, INFINITE);  
#else
    pthread_mutex_lock(&m_hMutex);
#endif
}

void ecMutex::UnLock()
{
#ifdef _WIN32
    ReleaseMutex(m_hMutex);   
#else
    pthread_mutex_unlock(&m_hMutex);
#endif
}
} /* namespace ec */